package test.adddata;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import javax.sql.DataSource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: gcr
 * @Date: 2022-3-30 13:24
 */
public class 合并新的抓取的数据doi {

    public static String fromTable = "xml_content_20200320_noexist";
    public static String fromTableParse = "pubmed_vip_extend_0328_33771437";
    public static String insertTable = "compare_doi";
    public static String insertTableSimple = "compare_doi2";

    @SneakyThrows
    public static void main(String[] args) {
        // -Xmx15660m -Xms8560m
        SmallTool.printMemory();

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
        Db use = Db.use(ds);
        String fromSql = "select pmid from " + fromTable;
        List<Entity> query = use.query(fromSql);
        List<String> pmidStr = query.stream().map(o -> o.getStr("pmid")).collect(Collectors.toList());
        List<Integer> pmidInt = query.stream().map(o -> o.getInt("pmid")).collect(Collectors.toList());
        SmallTool.printMemory();


        List<List<String>> split = ListUtil.split(pmidStr, 100);
        for (List<String> strings : split) {
            String pmiddoi = "select pmid ,doi from " + fromTableParse + " where pmid in (" + String.join(",", strings) + " )";
            List<Entity> queryPmidDoi = use.query(pmiddoi);
            SmallTool.printMemory();
            for (Entity entity : queryPmidDoi) {
                Integer pmidParam = entity.getInt("pmid");
                String doiParam = entity.getStr("doi");
                String sqlParam = " select * from " + insertTable + " where pmid = ?";
                Entity entity1 = use.queryOne(sqlParam, pmidParam);
                if (entity1 == null) {
                    Entity entity2 = new Entity();
                    entity2.setTableName(insertTable);
                    entity2.set("pmid", pmidParam);
                    entity2.set("doi_new", doiParam);
                    use.insert(entity2);
                } else {
                    entity1.set("doi_new", doiParam);
                    Entity where = new Entity();
                    where.setTableName(insertTable);
                    where.set("id", entity1.getInt("id"));
                    use.update(entity1, where);
                }

            }
        }

        for (List<String> strings : split) {
            //合并doi
            String paramSql = "select pmid,doi_new,doi_boss from " + insertTable + " where  pmid in  (" + String.join(",", strings) + " )";
            List<Entity> querys = use.query(paramSql);
            SmallTool.printMemory();
            querys.parallelStream().forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                String doiNew = entity.getStr("doi_new");
                String doiBoss = entity.getStr("doi_boss");
                String doiFinal = "";
                if (StringUtil.isNull(doiNew) && StringUtil.isNotNull(doiBoss)) {
                    doiFinal = doiBoss;
                }
                if (StringUtil.isNull(doiBoss) && StringUtil.isNotNull(doiNew)) {
                    doiFinal = doiNew;
                }
                if (StringUtil.isNotNull(doiNew) && StringUtil.isNotNull(doiBoss) && !doiNew.equals(doiBoss)) {
                    doiFinal = doiBoss;
                }
                if (StringUtil.isNotNull(doiNew) && StringUtil.isNotNull(doiBoss) && doiNew.equals(doiBoss)) {
                    doiFinal = doiNew;
                }
                // 进行更新到最新的doi上面去
                insertData(pmid, doiFinal, use);

            });


        }


    }

    @SneakyThrows
    public static void insertData(Integer pmid, String doi, Db use) {
        Entity whereEn = new Entity();
        whereEn.setTableName(insertTable);
        whereEn.set("pmid", pmid);
        use.update(new Entity()
                .set("doi_right", doi), whereEn
        );

    }
}
